package com.enqualcomm.kids.mvp.addwatch;


import com.enqualcomm.kids.R;
import com.enqualcomm.kids.bean.StringMessage;
import com.enqualcomm.kids.config.Constants;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.Presenter;

import de.greenrobot.event.EventBus;


/**
 * Created by yinzhiqun on 2015/10/12.
 *
 */
public class AddWatchPresenter implements EventHandler,Presenter<IAddWatchView> {
    private IAddWatchView view;
    private IAddWatchModel model;
    private final String userid;
    private final String userkey;
    private final boolean addWithCid;
    private final AppDefault appDefault;

    public AddWatchPresenter(boolean addWithCid) {
        model = new AddWatchModel();
        this.addWithCid = addWithCid;
        appDefault = new AppDefault();
        userid = appDefault.getUserid();
        userkey = appDefault.getUserkey();
    }

    /**
     *
     * @param cid
     * @param valid 是否检验cid
     */
    public void addWatch(String cid,boolean valid){
        if (addWithCid){
            if (valid && !cid.matches(Constants.CID_REGEX)) {
                view.showErrorMessage(R.string.cid_format_error);
                view.startScan();
                return;
            }
            view.showProgress();
            model.addWatchCid(cid, userkey, userid, this);
        }else{

            if (valid && !cid.matches(Constants.WID_REGEX)) {
                view.showErrorMessage(R.string.wid_format_error);
                view.startScan();
                return;
            }
            view.showProgress();
            model.addWatchWid(cid, userkey, userid, this);
        }
    }

    public void onStop(){
        model.onStop();
    }

    @Override
    public void attachView(IAddWatchView view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        view = null;
    }

    @Override
    public void onSuccessAdmin(String terminalid, String userterminalid) {
        //添加手表成功，管理员
        view.hideProgress();
        view.setWatchInfo(terminalid,userterminalid);
        //管理员添加时，不要发送通知，因为这个时候还没有设置名字号码等资料
    }

    @Override
    public void onSuccessOthers() {
        //其他人添加手表成功
        view.hideProgress();
        view.showSuccessDialog();
        EventBus.getDefault().post(new StringMessage(StringMessage.REFRESH_TERMINALLIST));
    }

    @Override
    public void onNetworkError() {
        view.hideProgress();
        view.showErrorMessage(R.string.network_error);
        view.startScan();
    }

    @Override
    public void onError(int errorCode,String adminPhoneNumber) {
        view.hideProgress();
        switch (errorCode){
            case -1:////管理员不允许添加
                view.showPromtDialog("添加失败","请联系"+adminPhoneNumber+"打开添加成员开关");
                break;
            case 5://重复添加
//                view.showErrorMessage(R.string.already_add_watch);
//                view.finishActivity();
                view.showPromtDialog("添加失败", "您已成功添加该手表，请不要重复添加");
                break;
            case 8://等待审批
//                view.showErrorMessage(R.string.wait_response_from_admin);
//                view.finishActivity();
                view.showPromtDialog("申请成功", "申请成功，请等待管理员确认");
                break;
            case 101://手表绑定人数已满
                view.showPromtDialog("添加失败", "手表绑定人数到达上限");
                break;
            default://cid,wid错误
//                view.showErrorMessage(addWithCid?R.string.cid_error:R.string.wid_error);
                view.showPromtDialog("添加失败", addWithCid?"非法的CID号码，无法添加":"非法的WID号码，无法添加");
                break;
        }
    }
}
